package com.hjm.project.merchantModelDetail.controller;
import com.hjm.common.utils.ExcelUtil;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.framework.web.page.TableDataInfo;
import com.hjm.project.merchantModelDetail.domain.MerchantModelDetail;
import com.hjm.project.merchantModelDetail.domain.MerchantModelDetailVo;
import com.hjm.project.merchantModelDetail.service.IMerchantModelDetailService;
import com.hjm.project.merchantModelDetail.utils.MerchantRateUtils;
import com.hjm.project.rateInfo.rateInfo.service.IRateInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 费率模版详情Controller
 * 
 * @author gdd
 */
@Controller
@RequestMapping("/merchant/merchantModelDetail")
public class MerchantModelDetailController extends BaseController {
    private String prefix = "merchant/merchantModelDetail";

    @Resource
    private IMerchantModelDetailService merchantModelDetailService;
    @Resource
    private IRateInfoService rateInfoService;

    @RequiresPermissions("merchant:MerchantModelDetail:view")
    @GetMapping()
    public String MerchantModelDetail() {
        return prefix + "/MerchantModelDetail";
    }

    /**
     * 查询费率模版详情列表
     */
    @RequiresPermissions("merchant:MerchantModelDetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MerchantModelDetail merchantModelDetail) {
        startPage();
        List<MerchantModelDetail> list = merchantModelDetailService.selectMerchantModelDetailList(merchantModelDetail);
        return getDataTable(list);
    }


    /**
     * 新增费率模版详情
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存费率模版详情
     */
    @RequiresPermissions("merchant:MerchantModelDetail:add")
    @Log(title = "费率模版详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MerchantModelDetail merchantModelDetail) {
        return toAjax(merchantModelDetailService.insertMerchantModelDetail(merchantModelDetail));
    }

    /**
     * 修改费率模版详情
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        MerchantModelDetail merchantModelDetail = merchantModelDetailService.selectMerchantModelDetailById(id);
        mmap.put("merchantModelDetail", merchantModelDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存费率模版详情
     */
    @RequiresPermissions("merchant:MerchantModelDetail:edit")
    @Log(title = "费率模版详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MerchantModelDetail merchantModelDetail) {
        return toAjax(merchantModelDetailService.updateMerchantModelDetail(merchantModelDetail));
    }

    /**
     * 删除费率模版详情
     */
    @RequiresPermissions("merchant:MerchantModelDetail:remove")
    @Log(title = "费率模版详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(merchantModelDetailService.deleteMerchantModelDetailByIds(ids));
    }


    /**
     * 保存费率模板详情
     * @return
     */
    @PostMapping("/saveSetModelDetailsRate")
    @ResponseBody
    public AjaxResult saveSetModelDetailsRate(@Valid MerchantModelDetailVo modelDetailVo) throws Exception {
        List<MerchantModelDetail> list = new ArrayList<>();

        try {
            //银行卡贷记卡D0
            String d0Fl = modelDetailVo.getYinhangkaDaijikaD0().toString();
            MerchantModelDetail daijikaD0 = MerchantRateUtils.merModelRateFormat(d0Fl, null, null,0, 2, 0);
            list.add(daijikaD0);
            //银行卡借记卡D0
            String j_d0Fl = modelDetailVo.getYinhangkaJiejikaD0().toString();;
            Integer j0_fd = modelDetailVo.getYinhangkaJiejikaD0Fd();
            MerchantModelDetail jiejikaD0 = MerchantRateUtils.merModelRateFormat(j_d0Fl, null, j0_fd.toString(),0, 1, 0);
            list.add(jiejikaD0);
            //云闪付贷记卡D0
            String ysf_d0Fl = modelDetailVo.getYunshanFuDaijikaD0().toString();
            MerchantModelDetail ysf_daijikaD0 = MerchantRateUtils.merModelRateFormat(ysf_d0Fl, null, null,1, 2, 0);
            list.add(ysf_daijikaD0);
            //云闪付借记卡D0
            String ysf_j_d0Fl = modelDetailVo.getYunshanFuJiejikaD0().toString();
            MerchantModelDetail ysf_jiejikaD0 = MerchantRateUtils.merModelRateFormat(ysf_j_d0Fl, null, null,1, 1, 0);
            list.add(ysf_jiejikaD0);

            //银联二维码贷记卡D0
            String qr_d0Fl = modelDetailVo.getQrDaijikaD0().toString();
            MerchantModelDetail qr_daijikaD0 = MerchantRateUtils.merModelRateFormat(qr_d0Fl, null, null,4, 2, 0);
            list.add(qr_daijikaD0);
            //银联二维码借记卡D0
            String qr_j_d0Fl = modelDetailVo.getQrJiejikaD0().toString();
            String qr_j_d0Fd = modelDetailVo.getQrJiejikaD0Fd().toString();
            MerchantModelDetail qr_jiejikaD0 = MerchantRateUtils.merModelRateFormat(qr_j_d0Fl, null, qr_j_d0Fd,4, 1, 0);
            list.add(qr_jiejikaD0);

            //微信借记卡D0
            String wx_j_d0Fl = modelDetailVo.getWeiXinJiejikaD0().toString();
            MerchantModelDetail wx_jiejikaD0 = MerchantRateUtils.merModelRateFormat(wx_j_d0Fl, null, null,2, 1, 0);
            list.add(wx_jiejikaD0);

            //支付宝借记卡D0
            String zfb_j_d0Fl = modelDetailVo.getZhifuBaoJiejikaD0().toString();
            MerchantModelDetail zfb_jiejikaD0 = MerchantRateUtils.merModelRateFormat(zfb_j_d0Fl, null, null,3, 1, 0);
            list.add(zfb_jiejikaD0);

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        merchantModelDetailService.saveModelDetailsRate(modelDetailVo,list);
        return success();
    }

    /**
     * 保存附加费率模板详情
     * @return
     */
    @PostMapping("/saveAddFj")
    @ResponseBody
    public AjaxResult saveAddFj(Integer fj,String modelNo) throws Exception {
        try {
            String fjCode = fj.toString();
            merchantModelDetailService.saveAddFj(fjCode,modelNo);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return success();
    }



}
